﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace RoadFlow.IntegrateTest.Common
{
    public class Tools
    {
        /// <summary>
        /// API地址
        /// </summary>
        public static readonly string ApiAddress = "http://localhost:50158";
        /// <summary>
        /// 当前登录用户ID
        /// </summary>
        public static readonly string CurrentUserId = "EB03262C-AB60-4BC6-A4C0-96E66A4229FE";
        /// <summary>
        /// 当前系统代码
        /// </summary>
        public static readonly string SystemCode = "localhost";

        /// <summary>
        /// 得到系统所有流程下拉选项
        /// </summary>
        /// <returns></returns>
        public static string GetFlowOptions(string flowId = "")
        {
            string url = ApiAddress + "/RoadFlowCoreApi/Flow/GetFlows";
            JObject jObject = new JObject
            {
                { "systemcode", SystemCode }
            };
            string json = HttpHelper.SendPost(url, jObject.ToString());
            JObject jObject1 = JObject.Parse(json);
            if (jObject1.Value<int>("errcode") != 0)
            {
                return string.Empty;
            }
            StringBuilder stringBuilder = new StringBuilder();
            foreach (JObject jObject2 in jObject1.Value<JArray>("data"))
            {
                string id = jObject2.Value<string>("Id");
                string Name = jObject2.Value<string>("Name");
                stringBuilder.Append("<option"+(id.Equals(flowId, StringComparison.CurrentCultureIgnoreCase) ? " selected='selected'" : "")+" value='" + id + "'>" + Name + "</option>");
            }
            return stringBuilder.ToString();
        }

        /// <summary>
        /// 得到系统所有流程Id
        /// </summary>
        /// <returns></returns>
        public static string GetFlowIds()
        {
            string url = ApiAddress + "/RoadFlowCoreApi/Flow/GetFlows";
            JObject jObject = new JObject
            {
                { "systemcode", SystemCode }
            };
            string json = HttpHelper.SendPost(url, jObject.ToString());
            JObject jObject1 = JObject.Parse(json);
            if (jObject1.Value<int>("errcode") != 0)
            {
                return string.Empty;
            }
            StringBuilder stringBuilder = new StringBuilder();
            foreach (JObject jObject2 in jObject1.Value<JArray>("data"))
            {
                string id = jObject2.Value<string>("Id");
                stringBuilder.Append(id);
                stringBuilder.Append(",");
            }
            return stringBuilder.ToString().TrimEnd(',');
        }

        /// <summary>
        /// 得到一个流程配置JSON
        /// </summary>
        /// <returns></returns>
        public static JObject GetFlowJson(Guid flowId)
        {
            string url = ApiAddress + "/RoadFlowCoreApi/Flow/GetFlowJson";
            JObject jObject = new JObject
            {
                { "systemcode", SystemCode },
                { "flowid", flowId.ToString() }
            };
            string json = HttpHelper.SendPost(url, jObject.ToString());
            JObject jObject1 = JObject.Parse(json);
            if (jObject1.Value<int>("errcode") != 0)
            {
                return null;
            }
            return JObject.Parse(json);
        }

        /// <summary>
        /// 得到一个步骤配置JSON
        /// </summary>
        /// <param name="flowId"></param>
        /// <param name="stepId"></param>
        /// <returns></returns>
        public static JObject GetStepJson(Guid flowId, Guid stepId)
        {
            JObject jObject = GetFlowJson(flowId);
            if (null == jObject)
            {
                return null;
            }
            JArray stepJArray = jObject.Value<JArray>("steps");
            foreach (JObject stepJObject in stepJArray)
            {
                if (stepJObject.Value<Guid>("id") == stepId)
                {
                    return stepJObject;
                }
            }
            return null;
        }

        /// <summary>
        /// 得到一个步骤的表单地址
        /// </summary>
        /// <param name="flowId"></param>
        /// <param name="stepId"></param>
        /// <returns></returns>
        public static Tuple<string, string> GetStepFormAddress(Guid flowId, Guid stepId)
        {
            string url = ApiAddress + "/RoadFlowCoreApi/Flow/GetStepFormAddress";
            JObject jObject = new JObject
            {
                { "systemcode", SystemCode },
                { "flowid", flowId.ToString() },
                { "stepid", stepId.ToString() }
            };
            string json = HttpHelper.SendPost(url, jObject.ToString());
            JObject jObject1 = JObject.Parse(json);
            if (jObject1.Value<int>("errcode") != 0)
            {
                return new Tuple<string, string>("", "");
            }
            return new Tuple<string, string>(jObject1.Value<string>("address"), jObject1.Value<string>("addressmobile"));
        }


        /// <summary>
        /// 得到处理类别显示标题
        /// </summary>
        /// <param name="executeType"></param>
        /// <returns></returns>
        public static string GetStatusTitle(int executeType)
        {
            string title = string.Empty;
            switch (executeType)
            {
                case -1:
                    title = "等待中";
                    break;
                case 0:
                    title = "未处理";
                    break;
                case 1:
                    title = "处理中";
                    break;
                case 2:
                    title = "已完成";
                    break;
                case 3:
                    title = "已退回";
                    break;
                case 4:
                    title = "他人已处理";
                    break;
                case 5:
                    title = "他人已退回";
                    break;
                case 6:
                    title = "已转交";
                    break;
                case 7:
                    title = "已委托";
                    break;
                case 8:
                    title = "已阅知";
                    break;
                case 9:
                    title = "已指派";
                    break;
                case 10:
                    title = "已跳转";
                    break;
                case 11:
                    title = "已终止";
                    break;
                case 12:
                    title = "他人已终止";
                    break;
                case 13:
                    title = "已加签";
                    break;
            }
            return title;
        }

        /// <summary>
        /// 得到显示流程图时的任务JSON
        /// </summary>
        /// <param name="tasks"></param>
        /// <returns></returns>
        public static JArray GetShowDesignerTaskJson(JArray tasks)
        {
            JArray taskJArray = new JArray();
            foreach (JObject task in tasks)
            {
                JObject taskJObject = new JObject
                {
                    { "stepid", task.Value<string>("StepId") },
                    { "prevstepid", task.Value<string>("PrevId") },
                    { "status", task.Value<string>("Status") },
                    { "sender", task.Value<string>("SenderName") },
                    { "receiver", task.Value<string>("ReceiveName") },
                    { "statustitle", "" }
                };
                taskJArray.Add(taskJObject);
            }
            return taskJArray;
        }
    }
}